package first.endtoend

import org.springframework.dao.DataIntegrityViolationException

class LocalityManagerController
{

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def filterPaneService


    def index()
    {
        redirect(action: "list", params: params)
    }

    def list(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        [localityManagerInstanceList: LocalityManager.list(params), localityManagerInstanceTotal: LocalityManager.count()]
    }


    def create()
    {
        println(params)

        def localityManagerInstance = new LocalityManager(params)
        def levelInstance
        if (params.level.id != null)
        {
            levelInstance = Level.findById(params.level.id)
        } else
        {
            //No level found
        }
        def localitiesByLevel = levelInstance.localities
        def adminInstance = Admin.findById(params.admin.id)
        return ['localityManagerInstance': localityManagerInstance, 'localitiesByLevel': localitiesByLevel, 'adminInstance': adminInstance]
/*
        [localityManagerInstance: new LocalityManager(params)]
*/

    }

    def save()
    {
        println(params)
        def localityManagerInstance = new LocalityManager(params)
        if (!localityManagerInstance.save(flush: true))
        {
            render(view: "create", model: [localityManagerInstance: localityManagerInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'localityManager.label', default: 'LocalityManager'), localityManagerInstance.id])
        redirect(action: "show", id: localityManagerInstance.id)
    }

    def show(Long id)
    {
        def localityManagerInstance = LocalityManager.get(id)
        if (!localityManagerInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'localityManager.label', default: 'LocalityManager'), id])
            redirect(action: "list")
            return
        }

        [localityManagerInstance: localityManagerInstance]
    }

    def edit(Long id)
    {
        def localityManagerInstance = LocalityManager.get(id)
        if (!localityManagerInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'localityManager.label', default: 'LocalityManager'), id])
            redirect(action: "list")
            return
        }
        [localityManagerInstance: localityManagerInstance]
    }

    def update(Long id, Long version)
    {
        def localityManagerInstance = LocalityManager.get(id)
        if (!localityManagerInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'localityManager.label', default: 'LocalityManager'), id])
            redirect(action: "list")
            return
        }

        if (version != null)
        {
            if (localityManagerInstance.version > version)
            {
                localityManagerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'localityManager.label', default: 'LocalityManager')] as Object[],
                        "Another user has updated this LocalityManager while you were editing")
                render(view: "edit", model: [localityManagerInstance: localityManagerInstance])
                return
            }
        }

        localityManagerInstance.properties = params

        if (!localityManagerInstance.save(flush: true))
        {
            render(view: "edit", model: [localityManagerInstance: localityManagerInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'localityManager.label', default: 'LocalityManager'), localityManagerInstance.id])
        redirect(action: "show", id: localityManagerInstance.id)
    }

    def delete(Long id)
    {
        def localityManagerInstance = LocalityManager.get(id)
        if (!localityManagerInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'localityManager.label', default: 'LocalityManager'), id])
            redirect(action: "list")
            return
        }

        try
        {
            localityManagerInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'localityManager.label', default: 'LocalityManager'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e)
        {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'localityManager.label', default: 'LocalityManager'), id])
            redirect(action: "show", id: id)
        }
    }


    def filter = {
        if (!params.max) params.max = 10
        render(view: 'list',
                model: [localityManagerInstanceList : filterPaneService.filter(params, LocalityManager),
                        localityManagerInstanceTotal: filterPaneService.count(params, LocalityManager),
                        filterParams                : org.grails.plugin.filterpane.FilterPaneUtils.extractFilterParams(params),
                        params                      : params])
    }
}
